<?php

class posController extends commonController {
    public function GetPosOrderDataAction()
    {
        $log = new Logging();
        $log->lfile('GetPosOrderData');
        $getOrderData = file_get_contents("php://input");
        $orderDatas = json_decode($getOrderData, true);
        $orderDatasT = json_encode($orderDatas, JSON_UNESCAPED_UNICODE);
        $resule = ['errcode' => 3];
        $checkCode = md5('shjieta');
        $checkCode = strtoupper(substr($checkCode, 0, 16));
        if ($checkCode !== $orderDatas['check_code']) {
            $resule['errormsg'] = '校验码错误';
            $resule = json_encode($resule, JSON_UNESCAPED_UNICODE);
            $log->lwrite('reception:' . $orderDatasT.'response:'.$resule);
            echo $resule;
            exit;
        }

        $r = $this->DuageIsNULL($orderDatas);
        if($r){
            $resule['errormsg'] = '字段'. $r .'必填参数不可为空';
            $resule = json_encode($resule, JSON_UNESCAPED_UNICODE);
            $log->lwrite('reception:' . $orderDatasT.'response:'.$resule);
            echo $resule;
            exit;
        }
        if($orderDatas['version'] == 10) {
            if (!trim($orderDatas['shop_no'])) {
                $resule['errormsg'] = '店铺编码不正确';
                $resule = json_encode($resule, JSON_UNESCAPED_UNICODE);
                $log->lwrite('reception:' . $orderDatasT.'response:'.$resule);
                echo $resule;
                exit;
            }
            $where = array("AND" => array("=" => array('order_no' => $orderDatas['order_no'], 'isdeleted' => 0)));
            $result = $this->model->getItemByOther($where, "orders");
            if($result && count((array)$result) > 0){
                $resule['errormsg'] = '该笔订单已存在';
                $resule = json_encode($resule, JSON_UNESCAPED_UNICODE);
                $log->lwrite('reception:' . $orderDatasT.'response:'.$resule);
                echo $resule;
                exit;
            }
            $itemOrdersp['order_no'] = $orderDatas['order_no'];
            $itemOrdersp['ph_no'] = 'POS' . $orderDatas['order_no'];
            $itemOrdersp['shop_no'] = $orderDatas['shop_no'];
            $itemOrdersp['shop_name'] = $orderDatas['shop_name'];
            $itemOrdersp['buyer'] = $orderDatas['buyer'];
            $itemOrdersp['buyer_mobile'] = $orderDatas['buyer_mobile'];
            $itemOrdersp['receiver'] = $orderDatas['receiver'];
            $itemOrdersp['receiver_tel'] = $orderDatas['receiver_tel'];
            $itemOrdersp['receiver_address'] = $orderDatas['receiver_address'];
            $itemOrdersp['order_time'] = $orderDatas['order_time'];
            $itemOrdersp['delivery_time'] = $orderDatas['delivery_time'];
            $itemOrdersp['actual_pay'] = $orderDatas['actual_pay'];
            $itemOrdersp['seller_remarks'] = $orderDatas['customre_memo'];
            $itemOrdersp['pos_fee'] = isset($orderDatas['pos_fee'])?$orderDatas['pos_fee']:0;
            $itemOrdersp['source'] = 'POS';
            $itemOrdersp['createdt'] = 'now()';
            $o = $this->model->addItem($itemOrdersp, 'orders');
            if (!$o) {
                $resule['errormsg'] = '系统异常';
                $resule = json_encode($resule, JSON_UNESCAPED_UNICODE);
                $log->lwrite('reception:' . $orderDatasT.'response:'.$resule);
                echo $resule;exit;
            }

            foreach ($orderDatas['goods_data'] as $g) {
                $itemGoods['order_no'] = $orderDatas['order_no'];
                $itemGoods['ph_no'] =  'POS' . $orderDatas['order_no'];
                $itemGoods['orders_id'] = $o;
                $itemGoods['product_no'] = $g['product_no'];
                $itemGoods['pastry'] = $g['pastry'];
                $itemGoods['delivery_type'] = $g['delivery_type'];
                $itemGoods['product_name'] = $g['product_name'];
                $itemGoods['qty'] = $g['qty'];
                $itemGoods['unit_price'] = $g['unit_price'];
                $itemGoods['amount'] = $g['amount'];
                $itemGoods['createdt'] = 'now()';

                $this->model->addItem($itemGoods, 'orders_product');

            }
            $resule['errcode'] = 2;
            $resule = json_encode($resule, JSON_UNESCAPED_UNICODE);
            $log->lwrite('reception:' . $orderDatasT.'response:'.$resule);
            echo $resule;exit;
        }else{
            $resule['errormsg'] = '未知版本';
            $resule = json_encode($resule, JSON_UNESCAPED_UNICODE);
            $log->lwrite('reception:' . $orderDatasT.'response:'.$resule);
            echo $resule;exit;
        }
    }

    private function DuageIsNULL(array $PosData){
        $duageOrder = ['order_no','shop_no','shop_name','receiver','receiver_tel','receiver_address','order_time','delivery_time','actual_pay'];
        $duageGoods = ['product_no','product_name','qty','unit_price','amount'];    # 商品详情 需要验证字段
        $t = '';
        foreach ($duageOrder as $p){
            if(!strlen(trim($PosData[$p])) > 0){
                $t = $p;
                break;
            }
        }

        if(!$t){
            foreach ($duageGoods as $pg){
                foreach ($PosData['goods_data'] as $g){
                    if(!strlen(trim($g[$pg])) > 0){
                        $t = $pg;
                        break;
                    }
                }

            }
        }

        return $t;
    }


    public function Pos_noAction(){
        $getPos_no = file_get_contents("php://input");
        $file = 'pos_no';
        self::WriteReception($file,$getPos_no);
        $Data = json_decode($getPos_no, true);

        if($_SERVER['REQUEST_METHOD'] !== 'POST'){
            self::WriteResponse($file,1,'Request type error','');
        }

        if($_SERVER['CONTENT_TYPE'] !== 'application/json'){
            self::WriteResponse($file,1,'Request data format error','');
        }

        if(!$Data || !is_array($Data)){
            self::WriteResponse($file,1,'数据结构异常','');
        }

        $validate = ['check_code'=>'check','version'=>'version','pos_no'=>'string','order_no'=>'string'];
        self::Verify($Data,$validate,$file,Data::$VERSION);

        $r = $this->model->EditOrder($Data);
        if($r['result']['status'] == 1){
            self::WriteResponse($file,$r['result']['status'],$r['result']['msg'],'');
        }
        self::WriteResponse($file,0,'','');

    }
}